package cn.androidlongs.study.sub.service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import cn.androidlongs.study.base.ResponeModel;
import cn.androidlongs.study.sub.dao.SubDao;
import cn.androidlongs.study.sub.vo.CommonItemSubjectModel;
import cn.androidlongs.study.sub.vo.CommonUserSubjectModel;
import cn.androidlongs.study.utils.DataUtils;
import cn.androidlongs.study.utils.GsonUtils;
import cn.androidlongs.study.utils.StringCodeUtils;

public class SubService {

    private SubDao subDao;


    public void setSubDao(SubDao subDao) {
	this.subDao = subDao;
    }
    public SubDao getSubDao() {
	return subDao;
    }

    /**
     *获取 所有的试题
     * @return
     */
    public String getAllCommonSubListFunction() {

	List<CommonUserSubjectModel> list = subDao.queryAllCommonSubList();

	ResponeModel<List<CommonUserSubjectModel>> responeModel = new ResponeModel<List<CommonUserSubjectModel> >();

	if (list!=null&&!list.isEmpty()) {
	    responeModel.content = list;
	    for (int i = 0; i < list.size(); i++) {
		CommonUserSubjectModel model = list.get(i);
		try {
		    if(model.uSubStartTime!=null&&!model.uSubStartTime.equals("")) {
			model.formatTime = DataUtils.getInstance().formatTime(Long.valueOf(model.uSubStartTime),DataUtils.DEFAULT_TIME_FORMAT);
		    }else {
			model.formatTime="无时间数据";
		    }
		} catch (Exception e) {
		    // TODO: handle exception
		}

	    }
	    responeModel.code="1000";
	    responeModel.message="获取试题成功 ";
	} else {
	    responeModel.code="1002";
	    responeModel.message="暂无试题信息 ";
	}

	String json ="";
	try {
	    json = GsonUtils.getintance().toJson(responeModel, new String[] {"itemSubmainSub"});
	} catch (Exception e) {
	    responeModel.code="1002";
	    responeModel.message="暂无试题信息 "+e.getMessage();
	    json = GsonUtils.getintance().toJson(responeModel,null);
	}
	System.out.println("获取完毕  "+json);
	return json;

    }
    /**
     * 获取试题详情 
     * @param subUUid
     * @return
     */
    public String getSubDetaileFunction(String subUUid) {


	ResponeModel<CommonUserSubjectModel> responeModel = new ResponeModel<CommonUserSubjectModel >();
	if (subUUid==null||subUUid.equals("")) {
	    responeModel.code="1003";
	    responeModel.message="uuid 为空 ";
	}else {

	    CommonUserSubjectModel queryModel = subDao.queryCommonSubFromUUID(subUUid);

	    if (queryModel!=null) {
		String uSubStartTime = queryModel.uSubStartTime;
		if (uSubStartTime!=null&&!uSubStartTime.equals("")) {
		    try {
			queryModel.formatTime = DataUtils.getInstance().formatTime(Long.valueOf(uSubStartTime), DataUtils.DEFAULT_TIME_FORMAT);
		    } catch (Exception e) {

		    }
		}
		responeModel.content = queryModel;
		responeModel.code="1000";
		responeModel.message="获取试题成功 ";
	    } else {
		responeModel.code="1002";
		responeModel.message="暂无试题信息 ";
	    }
	}

	String json ="";
	try {
	    json = GsonUtils.getintance().toJson(responeModel, new String[] {"itemSubmainSub"});
	} catch (Exception e) {
	    responeModel.code="1002";
	    responeModel.message="暂无试题信息 "+e.getMessage();
	    json = GsonUtils.getintance().toJson(responeModel,null);
	}
	System.out.println("获取完毕  "+json);
	return json;
    }
    /**
     * 删除小题、、、、
     * 先查询
     * 再删除
     * 
     * @param subUUid
     * @param subItemUUid
     * @return
     */
    public String deleteSubItemFunction(String subUUid, String subItemUUid) {

	ResponeModel<CommonUserSubjectModel> responeModel = new ResponeModel<CommonUserSubjectModel >();

	if (subItemUUid==null||subItemUUid.equals("")||subUUid==null||subUUid.equals("")) {
	    responeModel.code="1003";
	    responeModel.message="uuid 为空 ";
	} else {
	    CommonUserSubjectModel cSubjectModel = subDao.queryCommonSubFromUUID(subUUid);
	    Set<CommonItemSubjectModel> uSubItemList = cSubjectModel.uSubItemList;
	    CommonItemSubjectModel deleteModel=null;
	    for (CommonItemSubjectModel model : uSubItemList) {
		String itemUuid = model.itemUuid;
		if (itemUuid.equals(subItemUUid)) {
		    deleteModel = model;
		}
	    }
	    if (deleteModel!=null) {
		cSubjectModel.getuSubItemList().remove(deleteModel);
	    }

	    subDao.updateSubFunction(cSubjectModel);

	    responeModel.code="1000";
	    responeModel.message="已删除";

	}
	return GsonUtils.getintance().toJson(responeModel, null);
    }
    public String addSubItemFuncton(String subUUID, String itemTitle,
	    String itemSelectA, String itemSelectB, String itemSelectC,
	    String itemSelectD, String itemSelectS,int itemCount) {

	ResponeModel<CommonUserSubjectModel> responeModel = new ResponeModel<CommonUserSubjectModel >();

	if (subUUID==null||subUUID.equals("")) {
	    responeModel.code="1003";
	    responeModel.message="uuid 为空 ";
	} else {
	    CommonUserSubjectModel subjectModel = subDao.queryCommonSubFromUUID(subUUID);
	    if (subjectModel==null) {
		responeModel.code="1002";
		responeModel.message="试题不存在 ";
	    } else {
		responeModel.code="1000";
		responeModel.message="操作成功 ";
		CommonItemSubjectModel model = new CommonItemSubjectModel();
		model.itemSubTitle = StringCodeUtils.stringDecod(itemTitle);
		ArrayList<String> arrayList = new ArrayList<>();
		arrayList.add(StringCodeUtils.stringDecod(itemSelectA));
		arrayList.add(StringCodeUtils.stringDecod(itemSelectB));
		arrayList.add(StringCodeUtils.stringDecod(itemSelectC));
		arrayList.add(StringCodeUtils.stringDecod(itemSelectD));
		model.itemSubSelectList=arrayList;

		model.itemSubType=0;
		model.itemSubCount = itemCount;
		model.itemUuid=UUID.randomUUID().toString();
		model.itemSubmainSub = subjectModel;
		model.itemSubNormalSelect = itemSelectS;

		subjectModel.uSubItemList.add(model);

		subDao.updateSubFunction(subjectModel);

	    }
	}
	return GsonUtils.getintance().toJson(responeModel, null);
    }

    /**
     * 修改试题
     * @param subUUID
     * @param subFlagDes
     * @param subTitle
     * @return
     */
    public String alertSubFunction(String subUUID, String subFlagDes,
	    String subTitle) {

	ResponeModel<CommonUserSubjectModel> responeModel = new ResponeModel<CommonUserSubjectModel >();

	if (subUUID==null||subUUID.equals("")) {
	    responeModel.code="1003";
	    responeModel.message="uuid 为空 ";
	} else {
	    CommonUserSubjectModel subjectModel = subDao.queryCommonSubFromUUID(subUUID);
	    if (subjectModel==null) {
		responeModel.code="1002";
		responeModel.message="试题不存在 ";
	    } else {
		responeModel.code="1000";
		responeModel.message="操作成功 ";

		try {
		    subTitle = URLDecoder.decode(subTitle.trim(), "utf-8");
		} catch (UnsupportedEncodingException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}
		try {
		    subFlagDes = URLDecoder.decode(subFlagDes.trim(),"utf-8");
		} catch (UnsupportedEncodingException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}


		subjectModel.uSubName=subTitle;
		subjectModel.uSubPreface=subFlagDes;

		subDao.updateSubFunction(subjectModel);

	    }
	}
	return GsonUtils.getintance().toJson(responeModel, null);
    }

    /**
     * 添加试题 
     * @param subTitl
     * @param subSign
     * @return
     */
    public String addCommSubFunction(String subTitl, String subSign) {


	ResponeModel<CommonUserSubjectModel> responeModel = new ResponeModel<CommonUserSubjectModel >();

	if (subTitl==null||subTitl.equals("")) {
	    responeModel.code="1003";
	    responeModel.message="subTitl 为空 ";
	} else {
	    try {
		subTitl = URLDecoder.decode(subTitl, "utf-8");
	    } catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	    try {
		subSign = URLDecoder.decode(subSign, "utf-8");
	    } catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }


	    CommonUserSubjectModel subjectModel = new CommonUserSubjectModel();
	    subjectModel.uSubName = subTitl;
	    subjectModel.uSubPreface = subSign;
	    subjectModel.uSubUid = UUID.randomUUID().toString();
	    subjectModel.uSubStartTime=System.currentTimeMillis()+"";

	    subDao.addNewComSub(subjectModel);


	    responeModel.code="1000";
	    responeModel.message="操作成功 ";
	}
	return GsonUtils.getintance().toJson(responeModel, null);
    }
    /**
     * 删除试题 
     * @param subUUid
     * @return
     */
    public String deleteSubFunction(String subUUid) {

	ResponeModel<CommonUserSubjectModel> responeModel = new ResponeModel<CommonUserSubjectModel >();

	if (subUUid==null||subUUid.equals("")) {
	    responeModel.code="1003";
	    responeModel.message="subUUid 为空 ";
	} else {
	    CommonUserSubjectModel subjectModel = subDao.queryCommonSubFromUUID(subUUid);
	    if (subjectModel==null) {
		responeModel.code="1002";
		responeModel.message="试题不存在 ";
	    } else {
		responeModel.code="1000";
		responeModel.message="操作成功 ";

		try {
		    subUUid = URLDecoder.decode(subUUid.trim(), "utf-8");
		} catch (UnsupportedEncodingException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}
		
		subDao.deleteSubFunction(subjectModel);

	    }
	}
	return GsonUtils.getintance().toJson(responeModel, null);
    }



}
